﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Nav3.UIExcel;
using BAL.EstRec;
using Nav3;

namespace MasterDetailEx
{
    /// <summary>
    /// Lógica de interacción para DialogGeneraGridExel.xaml
    /// </summary>
    public partial class DialogGeneraGridExel : Window
    {
        DataTable DatosCorrectos;
        public CapturaFDV rfdv;
        public CapturaHq Hq;
        string Version;
        bool IsHQ;

        public void setVersion(string Version)
        {

            this.Version = Version;
        }

        public DialogGeneraGridExel()
        {
            InitializeComponent();

            DatosCorrectos = new DataTable();

            DataColumn col01 = new DataColumn("UNID_FDV_BGT");
            DatosCorrectos.Columns.Add(col01);
            DataColumn col02 = new DataColumn("ID_POS");
            DatosCorrectos.Columns.Add(col02);
            DataColumn col03 = new DataColumn("POS");
            DatosCorrectos.Columns.Add(col03);
            DataColumn col04 = new DataColumn("ID_RECURSO");
            DatosCorrectos.Columns.Add(col04);
            DataColumn col05 = new DataColumn("RECURSO");
            DatosCorrectos.Columns.Add(col05);
            DataColumn col06 = new DataColumn("CANTIDAD");
            DatosCorrectos.Columns.Add(col06);
            DataColumn col07 = new DataColumn("UNID_FDV_BGT_VERSION");
            DatosCorrectos.Columns.Add(col07);

            this.IsHQ = false;
        }

        public DialogGeneraGridExel(bool IsHQ)
        {
            InitializeComponent();

            DatosCorrectos = new DataTable();

            DataColumn col01 = new DataColumn("UNID_HQBGT");
            DatosCorrectos.Columns.Add(col01);
            DataColumn col02 = new DataColumn("ID_POS");
            DatosCorrectos.Columns.Add(col02);
            DataColumn col03 = new DataColumn("POS");
            DatosCorrectos.Columns.Add(col03);
            DataColumn col04 = new DataColumn("ID_RECURSO");
            DatosCorrectos.Columns.Add(col04);
            DataColumn col05 = new DataColumn("RECURSO");
            DatosCorrectos.Columns.Add(col05);
            DataColumn col06 = new DataColumn("CANTIDAD");
            DatosCorrectos.Columns.Add(col06);
            DataColumn col07 = new DataColumn("UNID_HQBGT_VERSION");
            DatosCorrectos.Columns.Add(col07);

            this.IsHQ = true;
        }

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        public void convertirExcelAGrid(string tabla)
        {
            DataTable ta;
            ta = TablaExcel.ExcelTabla(tabla);

            DataSet ds = new DataSet();

            int aux = 0;
            aux = ta.Rows.Count - 1;

            ds.Tables.Add(ta);
            dataGridExcel.ItemsSource = ds.Tables[0].DefaultView;
                      
            //dataGridExcel.SelectedIndex = aux - 1;
            //dataGridExcel.ScrollIntoView(ds.Tables[0].Rows[aux]);

            //for (int i = 0; i <= aux; i++) {

            //    dataGridExcel.ScrollIntoView(dataGridExcel.Items[i]);
            //}

            //dataGridExcel.ScrollIntoView(dataGridExcel.Items[0]);
            //dataGridExcel.ScrollIntoView(dataGridExcel.Items[aux]);
                        
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    DataRow drv;
            //    drv = ds.Tables[0].Rows[i];
            //    dataGridExcel.Items.Add(drv);

            //    DataRowView view = ds.Tables[0].DefaultView[i];
            //    DataGridRowEventArgs ee = new DataGridRowEventArgs(null);
            //    DisparaEvento(view, ee);
            //}

            DisparaEvento(ds.Tables[0]);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void dataGridExcel_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            DataRowView item = e.Row.Item as DataRowView;
            DisparaEvento(item, e);
        }

        public void DisparaEvento(DataTable TodosLosDatos)
        {
            string sPOS = "";
            string sRecursos = "";
            bool aux = false;

            metodosFDV mFDV = new metodosFDV();

            for (int i = 0; i < TodosLosDatos.Rows.Count; i++)
            {
                for (int j = 0; j < TodosLosDatos.Columns.Count; j++)
                {
                    if (j == 0)
                    {

                        sPOS = TodosLosDatos.Rows[i][j].ToString();

                        if (!mFDV.validarPOS2(sPOS))
                            aux = true;

                    }
                    else if (j == 1)
                    {

                        sRecursos = TodosLosDatos.Rows[i][j].ToString();

                        if (!mFDV.validaRecurso2(sRecursos))
                            aux = true;

                    }
                    else if (j == 2)
                    {


                        if (!IsHQ)
                        {
                            if (mFDV.validaRecPos2FDV(sPOS, sRecursos, Version))
                                aux = true;
                        }
                        try
                        {
                            int valorCantidad = 0;
                            valorCantidad = Int32.Parse(TodosLosDatos.Rows[i][j].ToString());
                        }
                        catch (Exception Ex)
                        {
                            aux = true;
                        }

                        if (!aux)
                        {

                            DataTable QComplete = mFDV.datosConVersionHQ(Version, sPOS, sRecursos);

                            int Colum = 0;
                            string dd1 = ""; string dd2 = ""; string dd3 = ""; string dd4 = ""; string dd5 = ""; string dd6 = ""; string dd7 = "";

                            if (QComplete.Rows.Count == 1)
                            {
                                foreach (DataRow ddrr in QComplete.Rows)
                                {
                                    foreach (DataColumn ddcc in QComplete.Columns)
                                    {
                                        if (Colum == 0)
                                            dd1 = ddrr.ItemArray[Colum].ToString();
                                        if (Colum == 1)
                                            dd2 = ddrr.ItemArray[Colum].ToString();
                                        if (Colum == 2)
                                            dd3 = ddrr.ItemArray[Colum].ToString();
                                        if (Colum == 3)
                                            dd5 = ddrr.ItemArray[Colum].ToString();
                                        if (Colum == 4)
                                            dd4 = ddrr.ItemArray[Colum].ToString();
                                        if (Colum == 5)
                                            dd6 = TodosLosDatos.Rows[i][j].ToString();
                                        if (Colum == 6)
                                            dd7 = ddrr.ItemArray[Colum].ToString();

                                        Colum += 1;
                                    }

                                    DataRow rowrow = DatosCorrectos.NewRow();
                                    rowrow[0] = dd1;
                                    rowrow[1] = dd2;
                                    rowrow[2] = dd3;
                                    rowrow[3] = dd4;
                                    rowrow[4] = dd5;
                                    rowrow[5] = dd6;
                                    rowrow[6] = dd7;
                                    DatosCorrectos.Rows.Add(rowrow);
                                }
                                aux = false;
                            }
                            else
                            {
                                DataRow rowrow = DatosCorrectos.NewRow();
                                rowrow[0] = "-1";
                                rowrow[1] = sPOS;
                                rowrow[2] = "-1";
                                rowrow[3] = sRecursos;
                                rowrow[4] = "-1";
                                rowrow[5] = TodosLosDatos.Rows[i][j].ToString();
                                rowrow[6] = Version;
                                DatosCorrectos.Rows.Add(rowrow);
                            }
                        }
                        else {

                            aux = false;
                        }
                    }
                }
            }
        }        
        
        public void DisparaEvento(DataRowView item, DataGridRowEventArgs e)
        {
            bool amarillo = false;

            if (IsHQ)
            {
                if (item != null)
                {
                    DataRow row = item.Row;
                    int contador = 1;

                    string sPOS = "";
                    string sRecursos = "";
                    bool aux = false;

                    foreach (string x in row.ItemArray)
                    {
                        metodosFDV mFDV = new metodosFDV();

                        if (contador == 1)
                        {

                            sPOS = x;

                            if (!mFDV.validarPOS2(sPOS))
                                aux = true;

                            contador++;
                        }
                        else if (contador == 2)
                        {

                            sRecursos = x;

                            if (!mFDV.validaRecurso2(sRecursos))
                                aux = true;

                            contador++;
                        }
                        else if (contador == 3)
                        {

                            //if (mFDV.validaRecPos2HQ(sPOS, sRecursos, Version))
                            //aux = true;

                            try
                            {
                                int valorCantidad = 0;
                                valorCantidad = Int32.Parse(x);
                            }
                            catch (Exception Ex)
                            {
                                aux = true;
                                amarillo = true;
                            }
                            if (aux)
                            {
                                e.Row.Background = new SolidColorBrush(Colors.Tomato);
                                if (amarillo)
                                    e.Row.Background = new SolidColorBrush(Colors.Yellow);
                            }
                            else
                            {
                                e.Row.Background = new SolidColorBrush(Colors.White);

                                DataTable QComplete = mFDV.datosConVersionHQ(Version, sPOS, sRecursos);

                                int Colum = 0;
                                string dd1 = ""; string dd2 = ""; string dd3 = ""; string dd4 = ""; string dd5 = ""; string dd6 = ""; string dd7 = "";

                                if (QComplete.Rows.Count == 1)
                                {
                                    foreach (DataRow ddrr in QComplete.Rows)
                                    {
                                        foreach (DataColumn ddcc in QComplete.Columns)
                                        {
                                            //Invertimos
                                            if (Colum == 0)
                                                dd1 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 1)
                                                dd2 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 2)
                                                dd3 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 3)
                                                dd5 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 4)
                                                dd4 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 5)
                                                dd6 = x;
                                            if (Colum == 6)
                                                dd7 = ddrr.ItemArray[Colum].ToString();

                                            Colum += 1;
                                        }

                                        DataRow rowrow = DatosCorrectos.NewRow();
                                        rowrow[0] = dd1;
                                        rowrow[1] = dd2;
                                        rowrow[2] = dd3;
                                        rowrow[3] = dd4;
                                        rowrow[4] = dd5;
                                        rowrow[5] = dd6;
                                        rowrow[6] = dd7;
                                        //DatosCorrectos.Rows.Add(rowrow);
                                    }
                                    contador = 1;
                                    aux = false;
                                }
                                else
                                {
                                    DataRow rowrow = DatosCorrectos.NewRow();
                                    rowrow[0] = "-1";
                                    rowrow[1] = sPOS;
                                    rowrow[2] = "-1";
                                    rowrow[3] = sRecursos;
                                    rowrow[4] = "-1";
                                    rowrow[5] = x;
                                    rowrow[6] = Version;
                                    //DatosCorrectos.Rows.Add(rowrow);
                                }
                            }
                        }
                    }
                }
                else
                {

                    //DataRowView item = e.Row.Item as DataRowView;
                    string idTerr, nomTerr, idRec, nomRec;
                    idTerr = null;
                    nomTerr = null;
                    idRec = null;
                    nomRec = null;
                    if (item != null)
                    {
                        DataRow row = item.Row;
                        int contador = 1;

                        string sPOS = "";
                        string sRecursos = "";
                        bool aux = false;

                        foreach (string x in row.ItemArray)
                        {
                            metodosFDV mFDV = new metodosFDV();

                            if (contador == 1)
                            {

                                sPOS = x;

                                if (!mFDV.validarPOS2(sPOS))
                                    aux = true;

                                contador++;
                            }
                            else if (contador == 2)
                            {

                                sRecursos = x;

                                if (!mFDV.validaRecurso2(sRecursos))
                                    aux = true;

                                contador++;
                            }
                            else if (contador == 3)
                            {

                                try
                                {
                                    int valorCantidad = 0;
                                    valorCantidad = Int32.Parse(x);
                                }
                                catch (Exception Ex)
                                {
                                    amarillo = true;
                                    aux = true;
                                }
                                //if (!IsHQ)
                                //{
                                //    if (!mFDV.validaRecPos2(sPOS, sRecursos, Version))
                                //        aux = true;
                                //}
                                if (aux)
                                {
                                    e.Row.Background = new SolidColorBrush(Colors.Tomato);
                                    if (amarillo)
                                        e.Row.Background = new SolidColorBrush(Colors.Yellow);
                                }
                                else
                                {
                                    e.Row.Background = new SolidColorBrush(Colors.Aqua);

                                    DataRow rowrow = DatosCorrectos.NewRow();
                                    rowrow[0] = sPOS;
                                    rowrow[1] = sRecursos;
                                    rowrow[2] = Int32.Parse(x);
                                    //DatosCorrectos.Rows.Add(rowrow);
                                }
                                contador = 1;
                                aux = false;
                            }
                        }
                    }
                }
            }
            else
            {

                if (item != null)
                {
                    DataRow row = item.Row;
                    int contador = 1;

                    string sPOS = "";
                    string sRecursos = "";
                    bool aux = false;

                    foreach (string x in row.ItemArray)
                    {
                        metodosFDV mFDV = new metodosFDV();

                        if (contador == 1)
                        {

                            sPOS = x;

                            if (!mFDV.validarPOS2(sPOS))
                                aux = true;

                            contador++;
                        }
                        else if (contador == 2)
                        {

                            sRecursos = x;

                            if (!mFDV.validaRecurso2(sRecursos))
                                aux = true;

                            contador++;
                        }
                        else if (contador == 3)
                        {

                            if (mFDV.validaRecPos2FDV(sPOS, sRecursos, Version))
                                aux = true;

                            try
                            {
                                int valorCantidad = 0;
                                valorCantidad = Int32.Parse(x);
                            }
                            catch (Exception Ex)
                            {
                                aux = true;
                                amarillo = true;
                            }
                            if (aux)
                            {
                                e.Row.Background = new SolidColorBrush(Colors.Tomato);
                                if (amarillo)
                                    e.Row.Background = new SolidColorBrush(Colors.Yellow);
                            }
                            else
                            {
                                e.Row.Background = new SolidColorBrush(Colors.White);

                                DataTable QComplete = mFDV.datosConVersionFDV(Version, sPOS, sRecursos);

                                int Colum = 0;
                                string dd1 = ""; string dd2 = ""; string dd3 = ""; string dd4 = ""; string dd5 = ""; string dd6 = ""; string dd7 = "";

                                if (QComplete.Rows.Count == 1)
                                {
                                    foreach (DataRow ddrr in QComplete.Rows)
                                    {
                                        foreach (DataColumn ddcc in QComplete.Columns)
                                        {

                                            if (Colum == 0)
                                                dd1 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 1)
                                                dd3 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 2)
                                                dd2 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 3)
                                                dd5 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 4)
                                                dd4 = ddrr.ItemArray[Colum].ToString();
                                            if (Colum == 5)
                                                dd6 = x;
                                            if (Colum == 6)
                                                dd7 = ddrr.ItemArray[Colum].ToString();

                                            Colum += 1;
                                        }

                                        DataRow rowrow = DatosCorrectos.NewRow();
                                        rowrow[0] = dd1;
                                        rowrow[1] = dd2;
                                        rowrow[2] = dd3;
                                        rowrow[3] = dd4;
                                        rowrow[4] = dd5;
                                        rowrow[5] = dd6;
                                        rowrow[6] = dd7;
                                        //DatosCorrectos.Rows.Add(rowrow);
                                    }
                                    contador = 1;
                                    aux = false;
                                }
                                else
                                {
                                    DataRow rowrow = DatosCorrectos.NewRow();
                                    rowrow[0] = "-1";
                                    rowrow[1] = sPOS;
                                    rowrow[2] = "-1";
                                    rowrow[3] = sRecursos;
                                    rowrow[4] = "-1";
                                    rowrow[5] = x;
                                    rowrow[6] = Version;
                                    //DatosCorrectos.Rows.Add(rowrow);
                                }
                            }
                        }
                    }
                }
                else
                {

                    //DataRowView item = e.Row.Item as DataRowView;
                    string idTerr, nomTerr, idRec, nomRec;
                    idTerr = null;
                    nomTerr = null;
                    idRec = null;
                    nomRec = null;
                    if (item != null)
                    {
                        DataRow row = item.Row;
                        int contador = 1;

                        string sPOS = "";
                        string sRecursos = "";
                        bool aux = false;

                        foreach (string x in row.ItemArray)
                        {
                            metodosFDV mFDV = new metodosFDV();

                            if (contador == 1)
                            {

                                sPOS = x;

                                if (!mFDV.validarPOS2(sPOS))
                                    aux = true;

                                contador++;
                            }
                            else if (contador == 2)
                            {

                                sRecursos = x;

                                if (!mFDV.validaRecurso2(sRecursos))
                                    aux = true;

                                contador++;
                            }
                            else if (contador == 3)
                            {

                                try
                                {
                                    int valorCantidad = 0;
                                    valorCantidad = Int32.Parse(x);
                                }
                                catch (Exception Ex)
                                {

                                    aux = true;
                                }
                                
                                if (!mFDV.validaRecPos2(sPOS, sRecursos, Version))
                                    aux = true;
                                
                                if (aux)
                                {
                                    e.Row.Background = new SolidColorBrush(Colors.Tomato);
                                }
                                else
                                {
                                    e.Row.Background = new SolidColorBrush(Colors.Aqua);

                                    DataRow rowrow = DatosCorrectos.NewRow();
                                    rowrow[0] = sPOS;
                                    rowrow[1] = sRecursos;
                                    rowrow[2] = Int32.Parse(x);
                                    //DatosCorrectos.Rows.Add(rowrow);
                                }
                                contador = 1;
                                aux = false;
                            }
                        }
                    }
                }
            }
        }

        private void btnAgregar_Click(object sender, RoutedEventArgs e)
        {
            if (IsHQ)
                this.Hq.InformacionDeExcel(DatosCorrectos);
            else
                this.rfdv.InformacionDeExcel(DatosCorrectos);

            this.Close();
        }

        private void dataGridExcel_Loaded(object sender, RoutedEventArgs e)
        {
            dataGridExcel.Columns[0].Width = 125;
            dataGridExcel.Columns[1].Width = 125;
            dataGridExcel.Columns[2].Width = 125;
        }
    }
}
